package cevent.source.cloudcenter.business.controller.admin;

import cevent.source.cloudcenter.server.dto.*;
import cevent.source.cloudcenter.server.service.ModuleService;
import cevent.source.cloudcenter.server.util.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cevent
 * @description 模块表控制层
 */
@RestController
@RequestMapping("/admin/module")
public class ModuleController {
    private static final Logger LOG = LoggerFactory.getLogger(ModuleController.class);
    //AOP日志环绕
    public static final String BUSINESS_NAME="模块管理 模块表";
    @Resource
    private ModuleService moduleService;

    //1.dto-module获取
    @RequestMapping("/dtoList")
    public List<ModuleDto> getDtoList() {
        return moduleService.moduleDtoList();
    }


    /*2.前端分页设置，后端pageInfo处理
    前端传参方式一：表单方式--》formData 前端：直接传对象   后端参数：(Dto对象)
    前端传参方式二：流的方式--》data{param1:value1,param2:vlaue2}  后端参数：(@RequestBody Dto dto对象)
     */
    @PostMapping("/pageList")
    public ResponseDataDto list(@RequestBody ModulePageDto modulePageDto) {
        LOG.info("查看controller的modulePageDto：{}", modulePageDto);
        ResponseDataDto responseData = new ResponseDataDto();
        moduleService.getModulePageDtoList(modulePageDto);
        responseData.setResponseData(modulePageDto);
        return responseData;
    }

    //3.新增，流方式传参，加入@RequestBody
    @PostMapping("/save")
    public ResponseDataDto save(@RequestBody ModuleDto moduleDto) {
        LOG.info("传入的module DTO：{}", moduleDto);

        ValidatorUtil.requiredLength(moduleDto.getName(), "模块名称", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getSeoTitle(), "SEO标题", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getSeoDescription(), "SEO描述", 3, 255);
        /*ValidatorUtil.requiredEmpty(moduleDto.getParentId(), "父ID");
        ValidatorUtil.requiredLength(moduleDto.getPicCover(), "模块封面", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getModulePath(), "模块路径", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getTemplatePath(), "引用模板路径", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getSubTemplatePath(), "引用页内模板路径", 3, 255);*/
        ResponseDataDto responseData = new ResponseDataDto();
        moduleService.save(moduleDto);
        responseData.setResponseData(moduleDto);
        return responseData;
    }

    //批量保存分类：模块分类关联dto
    @PostMapping("/saveMC")
    public ResponseDataDto saveMC(@RequestBody ModuleRelationCategoryDto moduleRelationCategoryDto) {
        LOG.info("传入的module DTO：{}", moduleRelationCategoryDto);

        ValidatorUtil.requiredLength(moduleRelationCategoryDto.getName(), "模块名称", 3, 255);
        ValidatorUtil.requiredLength(moduleRelationCategoryDto.getSeoTitle(), "SEO标题", 3, 255);
        ValidatorUtil.requiredLength(moduleRelationCategoryDto.getSeoDescription(), "SEO描述", 3, 255);

        if(!StringUtils.isEmpty(moduleRelationCategoryDto.getMaterialType()) && moduleRelationCategoryDto.getMaterialType().equals("EVENT")){
            LOG.info("获取到的模块类型：{}",moduleRelationCategoryDto.getMaterialType());
            ValidatorUtil.requiredEmpty(moduleRelationCategoryDto.getEventDate(),"大事记日期");
        }

        /*ValidatorUtil.requiredEmpty(moduleDto.getParentId(), "父ID");
        ValidatorUtil.requiredLength(moduleDto.getPicCover(), "模块封面", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getModulePath(), "模块路径", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getTemplatePath(), "引用模板路径", 3, 255);
        ValidatorUtil.requiredLength(moduleDto.getSubTemplatePath(), "引用页内模板路径", 3, 255);*/
        ResponseDataDto responseData = new ResponseDataDto();
        moduleService.saveMC(moduleRelationCategoryDto);
        responseData.setResponseData(moduleRelationCategoryDto);
        return responseData;
    }
    //根据moduleId保存picList
    @PostMapping("/savePicList")
    public ResponseDataDto savePicList(@RequestBody ModulePicPathDto modulePicPathDto){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(moduleService.savePicList(modulePicPathDto));
        return responseData;
    }
    //保存miniPicList
    @PostMapping("/saveMiniPicList")
    public ResponseDataDto saveMiniPicList(@RequestBody ModulePicPathDto modulePicPathDto){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(moduleService.saveMiniPicList(modulePicPathDto));
        return responseData;
    }
    //保存IconList
    @PostMapping("/saveIconList")
    public ResponseDataDto saveIconList(@RequestBody ModulePicPathDto modulePicPathDto){
        ResponseDataDto responseData=new ResponseDataDto();
        responseData.setResponseData(moduleService.saveIconList(modulePicPathDto));
        return responseData;
    }

    //根据module的uniId获取分类信息
    @PostMapping("/list-category/{moduleUniId}")
    public ResponseDataDto listCategory(@PathVariable(value="moduleUniId") String moduleUniId){
        ResponseDataDto responseData=new ResponseDataDto();
        List<CategoryModuleDto> categoryModuleDtoList= moduleService.getModuleCategory(moduleUniId);
        responseData.setResponseData(categoryModuleDtoList);
        return responseData;
    }

    /**
     * 指定请求的格式为Delete
     * 4.删除模块，如果为多个参数，就定义多个/{param}/{param}
     */
    @DeleteMapping("/del/{uniId}")
    public ResponseDataDto del(@PathVariable String uniId) {
        LOG.info("传入的module uniId：{}", uniId);
        ResponseDataDto responseData = new ResponseDataDto();
        moduleService.delete(uniId);
        return responseData;
    }

    //更新排序
    @RequestMapping("/sort")
    public ResponseDataDto sort(@RequestBody SortDto sortDto){
        LOG.info("传入的sortDto：{}",sortDto);
        ResponseDataDto responseData=new ResponseDataDto();
        moduleService.sort(sortDto);
        return responseData;
    }
}
